﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Liang.DataBase.Model.Entity;
using Liang.Model;
using Liang.UI;
using Liang.Util.Crypt;
using Liang.DataBase.BLL;
using Liang.DataBase.Tools;
using Liang.Util;
using Client.Helper;

namespace Client
{
    public partial class FrmUserEdit : Form
    {
        UserBLL userBLL = new UserBLL();

        IList<sys_role> allRoles = null;

        public sys_user user { get; set; }

        public FrmUserEdit()
        {
            InitializeComponent();
        }

        public FrmUserEdit(sys_user user)
        {
            InitializeComponent();
            this.user = user;
        }

        private void FrmUserEdit_Load(object sender, EventArgs e)
        {
            init();
        }

        public void init()
        {
            initcombo();

            if (user != null)
            {
                this.Text = "用户信息-修改";
                this.txtName.Text = user.loginName;
                this.txtName.ReadOnly = true;
                this.txtPersonName.Text = user.name;
                this.txtPwd.Text = AppHelper.DecPassword(user.password);
                this.comboBox_Sex.SelectedItem = user.sex;
                this.comboBox_Organ.SelectedValue = user.organId;
            }
            else 
            {
                this.Text = "用户信息-新增";
            }

            initUserRole();
        }

        void initcombo()
        {
            OrganBLL organBLL = new OrganBLL();
            this.comboBox_Organ.DataSource = organBLL.FindAll();
            this.comboBox_Organ.DisplayMember = "name";
            this.comboBox_Organ.ValueMember = "organId";
        }

        /// <summary>
        /// 初始化用户权限
        /// </summary>
        void initUserRole()
        {
            // 加载所有权限
            allRoles = userBLL.LoadAllRoles();

            this.treeView_role.BeginUpdate();
            this.treeView_role.Nodes.Clear();
            //TreeNode rootNode = this.treeView_role.Nodes.Add("用户权限");
            foreach (sys_role item in allRoles)
            {
                TreeNode t = this.treeView_role.Nodes.Add(item.name);
                t.Tag = item;
                if (null != user)
                {
                    if (null != user.roles.Where(p => p.roleId.Equals(item.roleId)))
                    {
                        t.Checked = true;
                    }
                }
            }
            this.treeView_role.ExpandAll();
            this.treeView_role.EndUpdate();
        }

        public bool validate()
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                MessageBoxEx.Show(this,"请输入账号。");
                this.txtName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtPwd.Text))
            {
                MessageBoxEx.Show(this,"请输入密码。");
                this.txtPwd.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtPersonName.Text))
            {
                MessageBoxEx.Show(this, "请输入姓名。");
                this.txtPersonName.Focus();
                return false;
            }

            return true;
        }

        /// <summary>
        /// 获取分配的用户权限
        /// </summary>
        /// <returns></returns>
        ISet<sys_role> getUserRoles()
        {
            ISet<sys_role> ret = new HashSet<sys_role>();

            foreach (TreeNode item in treeView_role.Nodes)
            {
                if (item.Checked)
                {
                    ret.Add((sys_role)item.Tag);
                }
            }

            return (0 == ret.Count) ? null : ret;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (validate())
            {
                if (user == null)
                {
                    user = new sys_user();
                    user.loginName = txtName.Text;
                    user.password = AppHelper.EncPassword(txtPwd.Text);
                    user.name = txtPersonName.Text;
                    user.roles = getUserRoles();
                    userBLL.Insert(user);
                }
                else
                {
                    sys_user tmp = userBLL.FindByID(user.userId);
                    tmp.loginName = txtName.Text;
                    tmp.password = AppHelper.EncPassword(txtPwd.Text);
                    tmp.name = txtPersonName.Text;
                    tmp.roles = getUserRoles();
                    userBLL.Update(tmp, tmp.userId);
                }

                DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
